package com.example.facecompare.user.img.service.impl;

import com.example.facecompare.user.img.entity.UserImg;
import com.example.facecompare.user.img.mapper.UserImgMapper;
import com.example.facecompare.user.img.service.IUserImgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tbb
 * @since 2023-07-31
 */
@Slf4j
@Service
public class UserImgServiceImpl extends ServiceImpl<UserImgMapper, UserImg> implements IUserImgService {

    public StringBuffer getCompareResult(String bashPath, String inputPath, String outputPath) throws Exception {
        StringBuffer list = new StringBuffer();
        log.info(bashPath);
//        log.info(inputPath);
        log.info(outputPath);

        String[] command = {"sh", bashPath, inputPath, outputPath};
        Process process = Runtime.getRuntime().exec(command);
        // 可选：获取脚本执行的输出结果
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            list.append(line);
            System.out.println(line);
        }
        int exitCode = process.waitFor();
        System.out.println("脚本执行完毕，退出码：" + exitCode);
        return list;
    }

//    public StringBuffer getCompareResult(String bashPath, String inputPath, String outputPath) throws Exception {
//        StringBuffer list = new StringBuffer();
//        // 构建要执行的Shell命令，包括Shell脚本和参数
//        ProcessBuilder processBuilder = new ProcessBuilder("sh", bashPath, inputPath, outputPath);
//
//        // 设置工作目录（如果需要）
////            processBuilder.directory(new File("/home/aisystem"));
//
//        // 启动Shell进程
//        Process process = processBuilder.start();
//        // 获取进程的输入流（标准输出流）
//        InputStream inputStream = process.getInputStream();
//        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
//
//        String line;
//        while ((line = reader.readLine()) != null) {
//            list.append(line);
//            System.out.println(line); // 打印每行输出
//        }
//        // 等待进程执行完成
//        int exitCode = process.waitFor();
//
//        if (exitCode == 0) {
//            System.out.println("Shell脚本执行成功");
//        } else {
//            System.err.println("Shell脚本执行失败，退出码: " + exitCode);
//        }
//        return list;
//    }
}
